package com.vvv.designpattern.singleton;

/**
 * @author vvv
 * @date 2023-11-13 09 22
 * discription
 * 相比于懒汉式，减小了锁的力度，并且也没有线程安全问题
 * volatile 是为了保证 doubleCheck = new DoubleCheck()时不会发生指令重排
 */
public class DoubleCheck {
    private DoubleCheck(){}
    private static volatile DoubleCheck doubleCheck = null;

    public static DoubleCheck getInstance() {
        if (doubleCheck != null) {
            return doubleCheck;
        }
        synchronized (DoubleCheck.class) {
            if (doubleCheck != null) {
                return doubleCheck;
            }
            doubleCheck = new DoubleCheck();
            return doubleCheck;
        }
    }


    public static void main(String[] args) {

    }
}
